package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.fi.constant.*;
import com.cinc.ebc.fi.entity.*;
import com.cinc.ebc.fi.mapper.*;
import com.cinc.ebc.fi.service.IFiReceiveVerifyService;
import com.cinc.ebc.mm.constant.StockInOutDetailVerifyStatus;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.mapper.MmStockInOutDetailMapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 收款核销
 * @Author: jeecg-boot
 * @Date: 2022-12-28
 * @Version: V1.0
 */
@Service
public class FiReceiveVerifyServiceImpl extends ServiceImpl<FiReceiveVerifyMapper, FiReceiveVerify> implements IFiReceiveVerifyService {

    @Autowired
    private FiReceiveVerifyMapper fiReceiveVerifyMapper;
    @Autowired
    private FiReceiveVerifyDetailMapper fiReceiveVerifyDetailMapper;

    @Autowired
    private FiReceiveVoucherMapper fiReceiveVoucherMapper;

    @Autowired
    private FiArOriginalAmountMapper fiArOriginalAmountMapper;

    @Autowired
    private MmStockInOutDetailMapper mmStockInOutDetailMapper;

    @Autowired
    private FiOtherReceiveDetailMapper fiOtherReceiveDetailMapper;

    @Autowired
    private FiOtherReceiveMapper fiOtherReceiveMapper;

    @Override
    @Transactional
    public void saveMain(FiReceiveVerify fiReceiveVerify, List<FiReceiveVerifyDetail> fiReceiveVerifyDetailList) {
        if (oConvertUtils.isEmpty(fiReceiveVerify.getSourceId())) {
            throw new RuntimeException("收款单不能为空");
        }
        BigDecimal verifyAmount = fiReceiveVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerify.getVerifyAmount();
        if (verifyAmount.compareTo(BigDecimal.ZERO) != 1) {
            throw new RuntimeException("核销金额必须大于零");
        }
        // 收款单处理
        handleVerifyVoucher(fiReceiveVerify);
        // 保存主表
        fiReceiveVerifyMapper.insert(fiReceiveVerify);

        // 保存核销明细
        if (fiReceiveVerifyDetailList != null && fiReceiveVerifyDetailList.size() > 0) {
            for (FiReceiveVerifyDetail entity : fiReceiveVerifyDetailList) {
                //外键设置
                entity.setMainId(fiReceiveVerify.getId());
                BigDecimal detailVerifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
                if (detailVerifyAmount.compareTo(BigDecimal.ZERO) != 1) {
                    throw new RuntimeException("明细核销金额必须大于零");
                }
                // 业务单据处理
                handleVerifyDetail(entity);
                //
                fiReceiveVerifyDetailMapper.insert(entity);
            }
        }
    }

    /**
     * 处理收款单
     *
     * @param fiReceiveVerify
     */
    private void handleVerifyVoucher(FiReceiveVerify fiReceiveVerify) {
        FiReceiveVerify fiReceiveVerifyHis = fiReceiveVerifyMapper.selectById(fiReceiveVerify.getId());
        if (ReceiveVerifyType.TYPE_03.equals(fiReceiveVerify.getVerifyType())) {
            // 期初预收款
            FiArOriginalAmount fiArOriginalAmount = fiArOriginalAmountMapper.selectById(fiReceiveVerify.getSourceId());
            if (AparInitialStatus.STATUS_10.equals(fiArOriginalAmount.getStatus())) {
                throw new RuntimeException("期初预收款未初始化，不能核销");
            }
            if (AparSettleStatus.STATUS_30.equals(fiArOriginalAmount.getSettleStatus())) {
                throw new RuntimeException("期初预收款已核销完成，不能核销");
            }
            BigDecimal originalVerifyAmount = fiArOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiArOriginalAmount.getVerifyAmount();
            BigDecimal verifyAmount = fiReceiveVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerify.getVerifyAmount();
            BigDecimal finalVerifyAmount = null;
            if (fiReceiveVerifyHis == null) {
                finalVerifyAmount = originalVerifyAmount.add(verifyAmount);
            } else {
                BigDecimal verifyAmountHis = fiReceiveVerifyHis.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerifyHis.getVerifyAmount();
                finalVerifyAmount = originalVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalVerifyAmount.compareTo(fiArOriginalAmount.getInitialBalance().abs()) == 1) {
                throw new RuntimeException("期初预收款累计核销金额不能超出预收款余额");
            }
            FiArOriginalAmount originalAmountModify = new FiArOriginalAmount();
            originalAmountModify.setId(fiReceiveVerify.getSourceId());
            originalAmountModify.setVerifyAmount(finalVerifyAmount);
            fiArOriginalAmountMapper.updateById(originalAmountModify);
        } else {
            // 预收款、直接收款
            FiReceiveVoucher fiReceiveVoucher = fiReceiveVoucherMapper.selectById(fiReceiveVerify.getSourceId());
            if (AparVoucherStatus.STATUS_01.equals(fiReceiveVoucher.getStatus())) {
                throw new RuntimeException("收款单未审核，不能核销");
            }
            if (AparVoucherStatus.STATUS_03.equals(fiReceiveVoucher.getStatus())) {
                throw new RuntimeException("收款单已核销完成，不能核销");
            }
            BigDecimal voucherVerifyAmount = fiReceiveVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getVerifyAmount();
            BigDecimal verifyAmount = fiReceiveVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerify.getVerifyAmount();
            BigDecimal finalVerifyAmount = null;
            if (fiReceiveVerifyHis == null) {
                finalVerifyAmount = voucherVerifyAmount.add(verifyAmount);
            } else {
                BigDecimal verifyAmountHis = fiReceiveVerifyHis.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerifyHis.getVerifyAmount();
                finalVerifyAmount = voucherVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalVerifyAmount.compareTo(fiReceiveVoucher.getActAmount().abs()) == 1) {
                throw new RuntimeException("收款单累计核销金额不能超出实收金额");
            }
            FiReceiveVoucher fiReceiveVoucherModify = new FiReceiveVoucher();
            fiReceiveVoucherModify.setId(fiReceiveVoucher.getId());
            fiReceiveVoucherModify.setVerifyAmount(finalVerifyAmount);
            fiReceiveVoucherModify.setUnverifyAmount(fiReceiveVoucher.getActAmount().subtract(finalVerifyAmount));
            fiReceiveVoucherMapper.updateById(fiReceiveVoucherModify);
        }
    }

    /**
     * 处理业务单据
     *
     * @param entity
     */
    private void handleVerifyDetail(FiReceiveVerifyDetail entity) {
        FiReceiveVerifyDetail entityHis = fiReceiveVerifyDetailMapper.selectById(entity.getId());
        if (AparVoucherDetailBillType.AR_01.equals(entity.getBillType())) {
            //销售出库单
            MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
            if (stockInOutDetail.getVerifyStatus().equals(StockInOutDetailVerifyStatus.STATUS_03)) {
                throw new RuntimeException("销售出库单已完成，不能再核销");
            }
            BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
            BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
            BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
            BigDecimal finalAmount = BigDecimal.ZERO;
            if (entityHis == null) {
                finalAmount = verifyAmount.add(stockVerifyAmount);
            } else {
                BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
                finalAmount = stockVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalAmount.compareTo(sumPrice) == 1) {
                throw new RuntimeException("核销金额不能大于单据金额");
            }
            MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
            stockInOutDetailModify.setId(entity.getBillId());
            stockInOutDetailModify.setVerifyDate(new Date());
            stockInOutDetailModify.setVerifyAmount(finalAmount);
            mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
        } else if (AparVoucherDetailBillType.AR_04.equals(entity.getBillType())) {
            // 应收期初余额
            FiArOriginalAmount fiArOriginalAmount = fiArOriginalAmountMapper.selectById(entity.getBillId());
            if (AparInitialStatus.STATUS_10.equals(fiArOriginalAmount.getStatus())) {
                throw new RuntimeException("期初应收未初始化，不能核销");
            }
            if (fiArOriginalAmount.getSettleStatus().equals(AparSettleStatus.STATUS_30)) {
                throw new RuntimeException("期初应收已结算完成，不能再核销");
            }
            BigDecimal originalVerifyAmount = fiArOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiArOriginalAmount.getVerifyAmount();
            BigDecimal initialBalance = fiArOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiArOriginalAmount.getInitialBalance();
            if (originalVerifyAmount.compareTo(initialBalance) == 1) {
                throw new RuntimeException("期初应收核销金额异常");
            }
            BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
            BigDecimal finalAmount = BigDecimal.ZERO;
            if (entityHis == null) {
                finalAmount = verifyAmount.add(originalVerifyAmount);
            } else {
                BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
                finalAmount = originalVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalAmount.compareTo(initialBalance) == 1) {
                throw new RuntimeException("核销金额不能大于期初余额");
            }
            FiArOriginalAmount fiArOriginalAmountModify = new FiArOriginalAmount();
            fiArOriginalAmountModify.setId(entity.getBillId());
            fiArOriginalAmountModify.setVerifyAmount(finalAmount);
            fiArOriginalAmountMapper.updateById(fiArOriginalAmountModify);
        } else if (AparVoucherDetailBillType.AR_02.equals(entity.getBillType())) {
            // 其他应收单
            FiOtherReceiveDetail fiOtherReceiveDetail = fiOtherReceiveDetailMapper.selectById(entity.getBillId());
            if (fiOtherReceiveDetail.getStatus().equals(AparOtherDetailStatus.STATUS_03)) {
                throw new RuntimeException("其他应收单已完成，不能再核销");
            }
            BigDecimal otherReceiveAmount = fiOtherReceiveDetail.getReceiveAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getReceiveAmount();
            BigDecimal otherVerifyAmount = fiOtherReceiveDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getVerifyAmount();
            BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
            BigDecimal finalAmount = BigDecimal.ZERO;
            if (entityHis == null) {
                finalAmount = verifyAmount.add(otherVerifyAmount);
            } else {
                BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
                finalAmount = otherVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalAmount.compareTo(otherReceiveAmount) == 1) {
                throw new RuntimeException("核销金额不能大于单据金额");
            }
            FiOtherReceiveDetail fiOtherReceiveDetailModify = new FiOtherReceiveDetail();
            fiOtherReceiveDetailModify.setId(entity.getBillId());
            fiOtherReceiveDetailModify.setVerifyAmount(finalAmount);
            fiOtherReceiveDetailMapper.updateById(fiOtherReceiveDetailModify);
            // 更新主表核销金额
            fiOtherReceiveMapper.autoUpdateVerifyAmount(fiOtherReceiveDetail.getMainId());
        } else {
            throw new RuntimeException("核销明细的单据类型异常，无法处理");
        }
    }

    @Override
    @Transactional
    public void updateMain(FiReceiveVerify fiReceiveVerify, List<FiReceiveVerifyDetail> fiReceiveVerifyDetailList) {
        FiReceiveVerify fiReceiveVerifyHis = fiReceiveVerifyMapper.selectById(fiReceiveVerify.getId());
        if ("1".equals(fiReceiveVerifyHis.getAuditFlag())) {
            throw new RuntimeException("已经审核，不能修改");
        }
        if (oConvertUtils.isEmpty(fiReceiveVerify.getSourceId())) {
            throw new RuntimeException("收款单不能为空");
        }
        BigDecimal verifyAmount = fiReceiveVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerify.getVerifyAmount();
        if (verifyAmount.compareTo(BigDecimal.ZERO) != 1) {
            throw new RuntimeException("核销金额必须大于零");
        }
        // 收款单处理
        handleVerifyVoucher(fiReceiveVerify);
        // 保存主表
        fiReceiveVerifyMapper.updateById(fiReceiveVerify);

        // 获取子表数据
        LambdaQueryWrapper<FiReceiveVerifyDetail> detailQuery = new LambdaQueryWrapper<>();
        detailQuery.eq(FiReceiveVerifyDetail::getMainId, fiReceiveVerify.getId());
        List<FiReceiveVerifyDetail> receiveVerifyDetailHisList = fiReceiveVerifyDetailMapper.selectList(detailQuery);
        List<String> detailIdList = receiveVerifyDetailHisList.stream().map(FiReceiveVerifyDetail::getId).collect(Collectors.toList());
        List<String> delIdList = detailIdList;
        // 子表数据更新
        if (oConvertUtils.listIsNotEmpty(fiReceiveVerifyDetailList)) {
            for (FiReceiveVerifyDetail entity : fiReceiveVerifyDetailList) {
                BigDecimal detailVerifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
                if (detailVerifyAmount.compareTo(BigDecimal.ZERO) != 1) {
                    throw new RuntimeException("核销金额必须大于零");
                }
                entity.setMainId(fiReceiveVerify.getId());
                //业务单据处理
                handleVerifyDetail(entity);
                //
                if (!detailIdList.contains(entity.getId())) {
                    //新增
                    fiReceiveVerifyDetailMapper.insert(entity);
                } else {
                    //修改
                    fiReceiveVerifyDetailMapper.updateById(entity);
                }
            }
            //获取删除记录ID
            List<String> curIdList = fiReceiveVerifyDetailList.stream().map(FiReceiveVerifyDetail::getId).collect(Collectors.toList());
            delIdList = detailIdList.stream().filter(id -> !curIdList.contains(id)).collect(Collectors.toList());
        }
        // 删除记录处理
        if (oConvertUtils.listIsNotEmpty(delIdList)) {
            for (String id : delIdList) {
                // 业务单据处理
                FiReceiveVerifyDetail entityHis = fiReceiveVerifyDetailMapper.selectById(id);
                FiReceiveVerifyDetail entityDel = new FiReceiveVerifyDetail();
                entityDel.setId(id);
                entityDel.setBillId(entityHis.getBillId());
                entityDel.setBillType(entityHis.getBillType());
                entityDel.setVerifyAmount(BigDecimal.ZERO);
                handleVerifyDetail(entityDel);
                // 删除
                fiReceiveVerifyDetailMapper.deleteById(id);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        FiReceiveVerify fiReceiveVerify = fiReceiveVerifyMapper.selectById(id);
        if ("1".equals(fiReceiveVerify.getAuditFlag())) {
            throw new RuntimeException("已经审核，不能删除");
        }
        List<FiReceiveVerifyDetail> fiReceiveVerifyDetailList = fiReceiveVerifyDetailMapper.selectByMainId(id);
        if (oConvertUtils.listIsNotEmpty(fiReceiveVerifyDetailList)) {
            for (FiReceiveVerifyDetail fiReceiveVerifyDetail : fiReceiveVerifyDetailList) {
                //处理业务单据
                FiReceiveVerifyDetail detailDel = new FiReceiveVerifyDetail();
                detailDel.setId(fiReceiveVerifyDetail.getId());
                detailDel.setBillId(fiReceiveVerifyDetail.getBillId());
                detailDel.setBillType(fiReceiveVerifyDetail.getBillType());
                detailDel.setVerifyAmount(BigDecimal.ZERO);
                handleVerifyDetail(detailDel);
            }
        }
        fiReceiveVerifyDetailMapper.deleteByMainId(id);
        fiReceiveVerifyMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            delMain(id.toString());
        }
    }

    @Transactional
    @Override
    public void audit(List<FiReceiveVerify> fiReceiveVerifyList) {
        for (FiReceiveVerify fiReceiveVerify : fiReceiveVerifyList) {
            FiReceiveVerify fiReceiveVerifyHis = fiReceiveVerifyMapper.selectById(fiReceiveVerify.getId());
            if ("1".equals(fiReceiveVerifyHis.getAuditFlag())) {
                throw new RuntimeException("已经审核，不能修改");
            }
            if (oConvertUtils.isEmpty(fiReceiveVerifyHis.getSourceId())) {
                throw new RuntimeException("收款单不能为空");
            }
            BigDecimal verifyAmount = fiReceiveVerifyHis.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVerifyHis.getVerifyAmount();
            if (verifyAmount.compareTo(BigDecimal.ZERO) != 1) {
                throw new RuntimeException("核销金额必须大于零");
            }
            FiReceiveVerify fiReceiveVerifyModify = new FiReceiveVerify();
            fiReceiveVerifyModify.setId(fiReceiveVerify.getId());
            fiReceiveVerifyModify.setAuditDate(new Date());
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            fiReceiveVerifyModify.setAuditor(sysUser != null ? sysUser.getUsername() : null);
            fiReceiveVerifyModify.setAuditFlag("1");
            fiReceiveVerifyMapper.updateById(fiReceiveVerifyModify);
            // 收款单状态处理
            handleVerifyVoucherStatus(fiReceiveVerify);
            // 业务单据状态处理
            List<FiReceiveVerifyDetail> fiReceiveVerifyDetailList = fiReceiveVerifyDetailMapper.selectByMainId(fiReceiveVerify.getId());
            for (FiReceiveVerifyDetail fiReceiveVerifyDetail : fiReceiveVerifyDetailList) {
                handleVerifyDetailStatus(fiReceiveVerifyDetail);
            }
        }
    }

    /**
     * 收款单状态处理
     *
     * @param fiReceiveVerify
     */
    private void handleVerifyVoucherStatus(FiReceiveVerify fiReceiveVerify) {
        if (ReceiveVerifyType.TYPE_03.equals(fiReceiveVerify.getVerifyType())) {
            // 期初预收款
            FiArOriginalAmount fiArOriginalAmount = fiArOriginalAmountMapper.selectById(fiReceiveVerify.getSourceId());
            if (AparInitialStatus.STATUS_10.equals(fiArOriginalAmount.getStatus())) {
                throw new RuntimeException("期初预收款未初始化");
            }
            BigDecimal originalVerifyAmount = fiArOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiArOriginalAmount.getVerifyAmount();
            BigDecimal initialBlance = fiArOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiArOriginalAmount.getInitialBalance().abs();
            if (originalVerifyAmount.compareTo(initialBlance) == 1) {
                throw new RuntimeException("期初预收款核销金额异常");
            }
            FiArOriginalAmount originalAmountModify = new FiArOriginalAmount();
            originalAmountModify.setId(fiReceiveVerify.getSourceId());
            if (originalVerifyAmount.compareTo(initialBlance) == 0) {
                originalAmountModify.setSettleStatus(AparSettleStatus.STATUS_30);
            } else if (originalVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                originalAmountModify.setSettleStatus(AparSettleStatus.STATUS_20);
            } else {
                originalAmountModify.setSettleStatus(AparSettleStatus.STATUS_10);
            }
            fiArOriginalAmountMapper.updateById(originalAmountModify);
        } else {
            // 预收款、直接收款
            FiReceiveVoucher fiReceiveVoucher = fiReceiveVoucherMapper.selectById(fiReceiveVerify.getSourceId());
            if (AparVoucherStatus.STATUS_01.equals(fiReceiveVoucher.getStatus())) {
                throw new RuntimeException("收款单未审核");
            }
            BigDecimal voucherVerifyAmount = fiReceiveVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getVerifyAmount();
            BigDecimal actAmount = fiReceiveVoucher.getActAmount() == null ? BigDecimal.ZERO : fiReceiveVoucher.getActAmount();
            if (voucherVerifyAmount.compareTo(actAmount) == 1) {
                throw new RuntimeException("收款单核销金额异常");
            }
            FiReceiveVoucher fiReceiveVoucherModify = new FiReceiveVoucher();
            fiReceiveVoucherModify.setId(fiReceiveVoucher.getId());
            if (voucherVerifyAmount.compareTo(actAmount) == 0) {
                fiReceiveVoucherModify.setStatus(AparVoucherStatus.STATUS_03);
            } else if (voucherVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                fiReceiveVoucherModify.setStatus(AparVoucherStatus.STATUS_02);
            } else {
                fiReceiveVoucherModify.setStatus(AparVoucherStatus.STATUS_01);
            }
            fiReceiveVoucherMapper.updateById(fiReceiveVoucherModify);
        }
    }

    /**
     * 业务单据状态处理
     *
     * @param entity
     */
    private void handleVerifyDetailStatus(FiReceiveVerifyDetail entity) {
        if (AparVoucherDetailBillType.AR_01.equals(entity.getBillType())) {
            //销售出库单
            MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
            BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
            BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
            if (stockVerifyAmount.compareTo(sumPrice) == 1) {
                throw new RuntimeException("销售出库单核销金额异常");
            }
            MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
            stockInOutDetailModify.setId(stockInOutDetail.getId());
            if (stockVerifyAmount.compareTo(sumPrice) == 0) {
                stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_03);
            } else if (stockVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_02);
            } else {
                stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_01);
            }
            mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
        } else if (AparVoucherDetailBillType.AR_04.equals(entity.getBillType())) {
            // 应收期初余额
            FiArOriginalAmount fiArOriginalAmount = fiArOriginalAmountMapper.selectById(entity.getBillId());
            BigDecimal initialBalance = fiArOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiArOriginalAmount.getInitialBalance();
            BigDecimal originalVerifyAmount = fiArOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiArOriginalAmount.getVerifyAmount();
            if (originalVerifyAmount.compareTo(initialBalance) == 1) {
                throw new RuntimeException("期初应收核销金额异常");
            }
            FiArOriginalAmount fiArOriginalAmountModify = new FiArOriginalAmount();
            fiArOriginalAmountModify.setId(fiArOriginalAmount.getId());
            if (originalVerifyAmount.compareTo(initialBalance) == 0) {
                fiArOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_30);
            } else if (originalVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                fiArOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_20);
            } else {
                fiArOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_10);
            }
            fiArOriginalAmountMapper.updateById(fiArOriginalAmountModify);
        } else if (AparVoucherDetailBillType.AR_02.equals(entity.getBillType())) {
            // 其他应收单
            FiOtherReceiveDetail fiOtherReceiveDetail = fiOtherReceiveDetailMapper.selectById(entity.getBillId());
            BigDecimal otherReceiveAmount = fiOtherReceiveDetail.getReceiveAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getReceiveAmount();
            BigDecimal otherVerifyAmount = fiOtherReceiveDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherReceiveDetail.getVerifyAmount();
            if (otherVerifyAmount.compareTo(otherReceiveAmount) == 1) {
                throw new RuntimeException("其他应收单核销金额异常");
            }
            FiOtherReceiveDetail fiOtherReceiveDetailModify = new FiOtherReceiveDetail();
            fiOtherReceiveDetailModify.setId(fiOtherReceiveDetail.getId());
            if (otherVerifyAmount.compareTo(otherReceiveAmount) == 0) {
                fiOtherReceiveDetailModify.setStatus(AparOtherDetailStatus.STATUS_03);
            } else if (otherVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                fiOtherReceiveDetailModify.setStatus(AparOtherDetailStatus.STATUS_02);
            } else {
                fiOtherReceiveDetailModify.setStatus(AparOtherDetailStatus.STATUS_01);
            }
            fiOtherReceiveDetailMapper.updateById(fiOtherReceiveDetailModify);
            // 更新主表状态
            fiOtherReceiveMapper.autoUpdateStatus(fiOtherReceiveDetail.getMainId());
        }
    }

}
